from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_plant.entity.do.task_do import PlantTask
from module_plant.entity.vo.task_vo import TaskModel, TaskPageQueryModel
from utils.page_util import PageUtil


class TaskDao:
    """
    种植任务明细模块数据库操作层
    """

    @classmethod
    async def get_task_detail_by_id(cls, db: AsyncSession, task_id: int):
        """
        根据任务Id获取种植任务明细详细信息

        :param db: orm对象
        :param task_id: 任务Id
        :return: 种植任务明细信息对象
        """
        task_info = (
            (
                await db.execute(
                    select(PlantTask)
                    .where(
                        PlantTask.task_id == task_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return task_info

    @classmethod
    async def get_task_detail_by_info(cls, db: AsyncSession, task: TaskModel):
        """
        根据种植任务明细参数获取种植任务明细信息

        :param db: orm对象
        :param task: 种植任务明细参数对象
        :return: 种植任务明细信息对象
        """
        task_info = (
            (
                await db.execute(
                    select(PlantTask).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return task_info

    @classmethod
    async def get_task_list(cls, db: AsyncSession, query_object: TaskPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取种植任务明细列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 种植任务明细列表信息对象
        """
        query = (
            select(PlantTask)
            .where(
                PlantTask.task_leader_id == query_object.task_leader_id if query_object.task_leader_id else True,
                PlantTask.task_type == query_object.task_type if query_object.task_type else True,
                PlantTask.scheduled_start_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_scheduled_start_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_scheduled_start_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_scheduled_start_time and query_object.end_scheduled_start_time
                else True,
                PlantTask.actual_start_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_actual_start_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_actual_start_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_actual_start_time and query_object.end_actual_start_time
                else True,
                PlantTask.plant_plot_id == query_object.plant_plot_id if query_object.plant_plot_id else True,
                PlantTask.planter_id == query_object.planter_id if query_object.planter_id else True,
                PlantTask.product_area_id == query_object.product_area_id if query_object.product_area_id else True,
                PlantTask.year == query_object.year if query_object.year else True,
                PlantTask.is_ontime == query_object.is_ontime if query_object.is_ontime else True,
            )
            .order_by(PlantTask.task_id)
            .distinct()
        )
        task_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return task_list

    @classmethod
    async def add_task_dao(cls, db: AsyncSession, task: TaskModel):
        """
        新增种植任务明细数据库操作

        :param db: orm对象
        :param task: 种植任务明细对象
        :return:
        """
        db_task = PlantTask(**task.model_dump(exclude={'task_id', 'actual_start_time', 'actual_end_time', }))
        db.add(db_task)
        await db.flush()

        return db_task

    @classmethod
    async def edit_task_dao(cls, db: AsyncSession, task: dict):
        """
        编辑种植任务明细数据库操作

        :param db: orm对象
        :param task: 需要更新的种植任务明细字典
        :return:
        """
        await db.execute(update(PlantTask), [task])

    @classmethod
    async def delete_task_dao(cls, db: AsyncSession, task: TaskModel):
        """
        删除种植任务明细数据库操作

        :param db: orm对象
        :param task: 种植任务明细对象
        :return:
        """
        await db.execute(delete(PlantTask).where(PlantTask.task_id.in_([task.task_id])))

